/**
 * 计算对应时间与当前相比的时间刻度
 * @param date time 要格式化显示的时间
 * 
 * 刚刚(3s以内)
    几秒前(10s以内)
    几分钟前(1分钟-60分钟)
    几小时前(1小时-24小时)
    几天前(1天-30天)
    几个月前(1个月-12个月之前)
    几年前(大于1年)
 */

function countTimeAgo(time){
    var now = new Date();
    //时间差, 单位为毫秒
    var timeDiff = Math.floor((now - time)/1000);   //转换成秒
    if(timeDiff <= 3){
        return "just a moment ago";
    }else if(timeDiff > 3 && timeDiff <= 10){
        return "before some seconds";
    }else if(timeDiff > 10 && timeDiff < 60){
        if(timeDiff === 1){
            return timeDiff+" second ago";
        }
        return timeDiff+" seconds ago";
    }else if(timeDiff >= 60 && timeDiff < 3600){
        if(Math.floor(timeDiff/60) === 1){
            return Math.floor(timeDiff/60)+" minute ago";
        }
        return Math.floor(timeDiff/60)+" minutes ago";
    }else if(timeDiff >= 3600 && timeDiff < 86400){
        if(Math.floor(timeDiff/3600) === 1){
            return Math.floor(timeDiff/3600)+" hour ago";
        }
        return Math.floor(timeDiff/3600)+" hours ago";
    }else if(timeDiff >= 86400 && timeDiff < 2592000){
        if(Math.floor(timeDiff/86400) === 1){
            return Math.floor(timeDiff/86400)+" day ago";
        }
        return Math.floor(timeDiff/86400)+" days ago";
    }else if(timeDiff >= 2592000 && timeDiff < 31104000){
        if(Math.floor(timeDiff/2592000) === 1){
            return Math.floor(timeDiff/2592000)+" month ago";
        }
        return Math.floor(timeDiff/2592000)+" months ago";
    }else if(timeDiff >= 31104000 ){
        if(Math.floor(timeDiff/31104000) === 1){
            return Math.floor(timeDiff/31104000)+" year ago";
        }
        return Math.floor(timeDiff/31104000)+" years ago";
    }
}


module.exports = countTimeAgo;